﻿using AutoMapper;
using MediatR;
using Reporting.API.Application.Commands.InvoiceManagements;
using Reporting.API.Application.Commands.Ticketmanagements;
using Reporting.Domain;
using Reporting.Errorcode.ErrorCode;
using Reporting.Infrastructure.Interfaces.Invoicetypes;
using Reporting.Infrastructure.Interfaces.Payees;
using Reporting.Infrastructure.Interfaces.Ticketmanagements;

namespace Reporting.API.Application.CommandHandler.Ticketmanagements
{
    public class GetIDTicketmanagementCommandHandler : IRequestHandler<GetIDTicketmanagementCommand, ApiResult<Ticketmanagement>>
    {
        private readonly ITicketmanagementRepository _ticketmanagementRepository;  //开票
        private readonly IMapper _mapper;
        private readonly ILogger<GetIDTicketmanagementCommandHandler> _logger;

        public GetIDTicketmanagementCommandHandler(ILogger<GetIDTicketmanagementCommandHandler> logger, IMapper mapper, ITicketmanagementRepository ticketmanagementRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _ticketmanagementRepository = ticketmanagementRepository;
        }

        public async Task<ApiResult<Ticketmanagement>> Handle(GetIDTicketmanagementCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 获取所有到票
                var InvoiceManagements = await _ticketmanagementRepository.GetAsync(x => x.Ticketmanagementid == request.Ticketmanagementid);

                // 使用 AutoMapper 进行对象映射
                var InvoiceManagementReposnse = _mapper.Map<Ticketmanagement>(InvoiceManagements);

                // 返回成功结果
                return ApiResult<Ticketmanagement>.Success(InvoiceManagementReposnse, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询失败");

                // 返回失败结果
                return ApiResult<Ticketmanagement>.Fail("查询失败：" + ex.Message, StatusCodeEnum.BadRequest);
            }
        }
    }
}
